package xin.marcher.module.common.redis;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * </p>
 */
@Data
@Configuration
//@ConditionalOnClass(RedisConnectionFactory.class)
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

//    @Value("${spring.redis.password}")
//    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${guava.cache.initial-capacity:500}")
    private int initialCapacity;

    @Value("${guava.cache.maximum-size:10000}")
    private int maximumSize;

    @Value("${guava.cache.expire-after-write:43200}")
    private int expireAfterWrite;

    @Value("${guava.cache.expire-after-access:43200}")
    private int expireAfterAccess;

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
//    @ConditionalOnClass(RedisConnectionFactory.class)
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
//    @ConditionalOnClass(RedisConnectionFactory.class)
    public Cache cache() {
        return CacheBuilder.newBuilder()
                // 设置并发级别为cpu核心数
                .concurrencyLevel(Runtime.getRuntime().availableProcessors())
                // 设置初始容量
                .initialCapacity(initialCapacity)
                // 设置最大存储
                .maximumSize(maximumSize)
                // 写缓存后多久过期
                .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
                // 读写缓存后多久过期
                .expireAfterAccess(expireAfterAccess, TimeUnit.SECONDS)
                .build();
    }

    @Bean
    @ConditionalOnClass(RedissonClient.class)
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
//                .setPassword(password)
                .setConnectionMinimumIdleSize(10)
                .setConnectionPoolSize(100)
                .setIdleConnectionTimeout(600000)
                .setSubscriptionConnectionMinimumIdleSize(10)
                .setSubscriptionConnectionPoolSize(100)
                .setTimeout(timeout);

        config.setCodec(new StringCodec());
        config.setThreads(5);
        config.setNettyThreads(5);

        RedissonClient client = Redisson.create(config);

        return client;
    }

    @Bean
//    @ConditionalOnClass(RedisConnectionFactory.class)
    public RedisCache redisCache(RedisTemplate redisTemplate, Cache cache) {
        return new RedisCache(redisTemplate, cache);
    }

    @Bean
    @ConditionalOnClass(RedissonClient.class)
    public RedisLock redisLock(RedissonClient redissonClient) {
        return new RedisLock(redissonClient);
    }

    @Bean
    @ConditionalOnClass(RedissonClient.class)
    public RedisBloom redisBloom(RedissonClient redissonClient) {
        return new RedisBloom(redissonClient);
    }
}
